#include "FrameView.h"
#include "ToolPane.h"
#include <wx/dcmemory.h>
#include "set.h"
#include "global.h"
#include <wx/msgdlg.h>
//(*InternalHeaders(FrameView)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//(*IdInit(FrameView)
//*)

BEGIN_EVENT_TABLE(FrameView,wxPanel)
    //(*EventTable(FrameView)
    //*)
    EVT_PAINT(FrameView::OnPaint)
    EVT_ERASE_BACKGROUND(FrameView::OnBackground)
    EVT_TIMER(-1,FrameView::OnTimer)
END_EVENT_TABLE()

//IsShownOnScreen

void FrameView::OnTimer(wxTimerEvent &ev)
{
    if(!IsShownOnScreen())
    {
        m_timer.Stop();
        //wxMessageBox("timer stop");
    }
    else
    {
        Refresh();
    }
}

void FrameView::OnBackground(wxEraseEvent &event)
{
    //event.Skip();
}

void FrameView::Draw(wxDC &dc)
{
    wxSize gridSize = GetGridSize();
    wxSize size = GetScreenSize();

    for(int i = 0; i < m_frameNum; i++)
    {
        m_pFrames[i]->Draw(dc);
    }

    if(m_frameNum > 0)
    {
        dc.SetBrush(*wxLIGHT_GREY_BRUSH);
        dc.SetPen(*wxLIGHT_GREY_PEN);
        dc.SetLogicalFunction(wxAND);
        dc.DrawRectangle(GetClientSize());
    }
    DrawFrame(dc,*m_pFrame);
    for(vector<SFrame>::iterator i = m_frameList.begin(); i != m_frameList.end(); i++)
    {
        DrawFrame(dc,*i);
    }
    for(vector<SAction>::iterator i = m_actionList.begin(); i != m_actionList.end(); i++)
    {
        i->Draw(dc);
    }

    dc.SetBrush(*wxGREEN_BRUSH);
    dc.SetPen(*wxGREEN_PEN);
    dc.SetLogicalFunction(wxCOPY);

    if(ShowGird())
    {
        for(int i = 0; i <= size.GetWidth()/gridSize.GetWidth(); i++)
        {
            dc.DrawLine(i*gridSize.GetWidth(),0,i*gridSize.GetWidth(),size.GetHeight());
        }
        for(int i = 0; i <= size.GetHeight()/gridSize.GetHeight(); i++)
        {
            dc.DrawLine(0,i*gridSize.GetHeight(),size.GetWidth(),i*gridSize.GetHeight());
        }
    }
}

void FrameView::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);
    wxSize clientSize = GetClientSize();
    wxBitmap bmp(clientSize.GetWidth(),clientSize.GetHeight());
    wxMemoryDC memDC(bmp);

    memDC.SetBrush(*wxWHITE_BRUSH);
    memDC.SetPen(*wxWHITE_PEN);
    memDC.DrawRectangle(GetClientSize());

    Draw(memDC);

    switch(m_selType)
    {
    case ESTImage:
    {
        if(m_selInd >= m_pFrame->m_imageList.size())
            break;
        wxRect rect = m_pFrame->m_imageList[m_selInd].GetRect();
        memDC.SetBrush(*wxTRANSPARENT_BRUSH);
        memDC.SetPen(*wxRED_PEN);
        memDC.DrawRectangle(rect);
    }
    break;
    case ESTFrame:
    {
        if(m_selInd >= m_frameList.size())
            break;
        wxRect rect = m_frameList[m_selInd].GetRect();
        memDC.SetBrush(*wxTRANSPARENT_BRUSH);
        memDC.SetPen(*wxRED_PEN);
        memDC.DrawRectangle(rect);
    }
    break;
    case ESTAction:
    {
        if(m_selInd >= m_actionList.size())
            break;
        wxRect rect = m_actionList[m_selInd].GetRect();
        memDC.SetBrush(*wxTRANSPARENT_BRUSH);
        memDC.SetPen(*wxRED_PEN);
        memDC.DrawRectangle(rect);
    }
    break;
    }
    dc.Blit(0,0,clientSize.GetWidth(),clientSize.GetHeight(),&memDC,0,0);

    if((m_actionList.size() > 0) && (!m_timer.IsRunning()))
    {
        m_timer.SetOwner(this);
        m_timer.Start(100);
    }
}

FrameView::FrameView(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
    m_selType = 0;
    m_pFrames = NULL;
    m_frameNum = 0;
    m_pFrame = new SFrame;
    //(*Initialize(FrameView)
    Create(parent, id, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("id"));
    //*)
}

void FrameView::AddImage(wxString &fileName,wxRect &rect,wxPoint &point)
{
    SFrameImage frameImage;
    frameImage.name = fileName;
    frameImage.rect = rect;
    frameImage.point = point;
    m_pFrame->m_imageList.push_back(frameImage);
}

void FrameView::AddFrame(SFrame &frame)
{
    m_frameList.push_back(frame);
}
void FrameView::AddAction(SAction &action)
{
    action.m_curFrame = 0;
    m_actionList.push_back(action);
}

SAction *FrameView::GetAction()
{
    if(m_actionList.size() <= 0)
        return NULL;
    return &(m_actionList[0]);
}

vector<SFrame> *FrameView::GetFrameList()
{
    return &m_frameList;
}
vector<SAction> *FrameView::GetActionList()
{
    return &m_actionList;
}

FrameView::~FrameView()
{
    delete m_pFrame;
    //(*Destroy(FrameView)
    //*)
}

void FrameView::ClearImage()
{
    m_pFrame->m_imageList.clear();
}
